﻿package Socket
{
	import flash.net.Socket;
	import flash.events.ProgressEvent;
	import Game.MatchType;
	import Game.Player;
	import Component.Duel;
	import Component.Bomb;
	import Component.DuelScreen;
	import Game.Team;
	import Component.SoloScore;
	import Component.MeleeScreen;
	import Component.DuoScore;
	import Component.FFAScore;

	public class ActionListener extends Contexted
	{
		private var socket:Socket;

		public function ActionListener(context:Context)
		{
			super(context);
			this.socket = context.connection.socket;
			socket.addEventListener(ProgressEvent.SOCKET_DATA , this.dataHandler);
		}

		function dataHandler(event:ProgressEvent):void
		{
			while (socket.bytesAvailable > 0)
			{
				//trace("socketDataHandler: " + event);
				var header:int = socket.readByte();
				switch (header)
				{
					case Headers.PlayerMove :
						var player_id:int = socket.readUnsignedShort();
						var player_x:int = 2 * socket.readUnsignedShort();
						var player_y:int = 2 * socket.readUnsignedShort();
						trace("Player "+player_id+" moved : ["+player_x+"/"+player_y+"]");
						context.map.move(player_id,player_x,player_y);
						break;
					case Headers.Initialize :
						trace("Initialize");
						context.match.initialize();
						switch (socket.readByte())
						{
							case MatchType.Solo :
								trace("MatchType : Solo");
								context.match.addPlayer(new Player(0,socket.readUTF()),0);
								context.match.addPlayer(new Player(1,socket.readUTF()),1);
								context.screen.map = new DuelScreen();
								context.screen.score = new SoloScore();
								break;
							case MatchType.Team :
								trace("MatchType : Team");
								context.match.addPlayer(new Player(0,socket.readUTF()),0);
								context.match.addPlayer(new Player(1,socket.readUTF()),0);
								context.match.addPlayer(new Player(2,socket.readUTF()),1);
								context.match.addPlayer(new Player(3,socket.readUTF()),1);
								context.screen.map = new MeleeScreen();
								context.screen.score = new DuoScore();
								break;
							case MatchType.FFA :
								trace("MatchType : FFA");
								context.match.addPlayer(new Player(0,socket.readUTF()),0);
								context.match.addPlayer(new Player(1,socket.readUTF()),1);
								context.match.addPlayer(new Player(2,socket.readUTF()),2);
								context.match.addPlayer(new Player(3,socket.readUTF()),3);
								context.screen.map = new MeleeScreen();
								context.screen.score = new FFAScore();
								break;
						}
						context.map = context.screen.map.map;
						context.map.initialize(context);
						context.screen.load_screen.update();
						context.screen.display_load_screen();
						break;
					case Headers.Identified :
						trace("Identified");
						context.player = context.match.getPlayer(socket.readUnsignedShort());
						trace("You are Player : " + context.player.name);
						context.sender.onReady();
						break;
					case Headers.PlayerReady :
						var id:uint = socket.readUnsignedShort();
						context.screen.load_screen.ready(id);
						trace("Player is Ready : " + id);
						break;
					case Headers.MapReset :
						var seed:uint = socket.readUnsignedInt();
						trace("MapReset seed : " + seed);
						context.randomizer.reset(seed);
						context.map.reset();
						context.screen.map.new_round();
						context.screen.display_map();
						break;
					case Headers.BombSet :
						var player:uint = socket.readUnsignedShort();
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						var bomb:Bomb = context.map.set_bomb(x,y);
						context.match.getPlayer(player).attach(bomb);
						trace("Bomb set : ["+x+"/"+y+"]");
						break;
					case Headers.BombExplode :
						var player:uint = socket.readUnsignedShort();
						var length:uint = socket.readUnsignedShort();
						trace("Boom ! lengt : " + length + " /player : " + player);
						// remove the bomb from the player
						var bomb:Bomb = context.match.getPlayer(player).explode();
						// boom
						context.map.explode(bomb,length);
						break;
					case Headers.BlockDestroyed :
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						trace("Destroyed : ["+x+"/"+y+"]");
						context.map.destroyed(x,y);
						break;
					case Headers.BonusSpawn :
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						trace("Bonus Spawn : ["+x+"/"+y+"]");
						context.map.getBonus(x,y).visible = true;
						break;
					case Headers.BonusDrop :
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						trace("Bonus Drop : ["+x+"/"+y+"]");
						context.map.set_bonus_perm(x,y);
						break;
					case Headers.BonusTaken :
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						trace("Bonus Taken : ["+x+"/"+y+"]");
						context.map.getBonus(x,y).destroy();
						break;
					case Headers.PlayerKilled :
						var player:uint = socket.readUnsignedShort();
						context.match.getPlayer(player).avatar.visible = false;
						trace("Player Killed : " + player);
						context.screen.map.playerDie(player);
						break;
					case Headers.ScoreBoard :
						trace("ScoreBoard teams : "+context.match.team_number+", players : "+context.match.player_number);
						var index:uint;
						for( index = 0; index < context.match.team_number; index++){
							context.screen.score.setTeamStats(index,socket.readShort());
						}
						for( index = 0; index < context.match.player_number; index++){
							context.screen.score.setPlayerStats(index,socket.readUnsignedShort(),socket.readUnsignedShort());						
						}
						context.screen.display_score_board();
						break;
					case Headers.EndGame :
						var player_id:int = socket.readUnsignedShort();
						trace("EndGame winner : "+ player_id);
						context.screen.score.isWinner(context.player.id == player_id);
						var index:uint;
						for( index = 0; index < context.match.team_number; index++){
							context.screen.score.setTeamStats(index,socket.readShort());
						}
						for( index = 0; index < context.match.player_number; index++){
							context.screen.score.setPlayerStats(index,socket.readUnsignedShort(),socket.readUnsignedShort());						
						}
						context.screen.display_score_board();
						break;
					case Headers.BombKicked :
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						var direction:uint = socket.readUnsignedShort();
						context.map.bomb_kicked(x,y,direction);
						trace("Bomb Kicked : ["+x+"/"+y+"]");
						break;
					case Headers.SlabOff :
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						trace("Slab Off : ["+x+"/"+y+"]");
						context.map.getSlab(x,y).disactivate();
						break;
					case Headers.SlabOn :
						var x:uint = socket.readUnsignedShort();
						var y:uint = socket.readUnsignedShort();
						trace("Slab On : ["+x+"/"+y+"]");
						context.map.getSlab(x,y).activate();
						break;
					default :
						trace("Paquet Inconnu : 0x" + header.toString(16));
						socket.flush();
						break;
				}
			}
		}

	}

}